package com.dao.impl;

import com.bean.Viedo;
import com.dao.BaseDao;
import com.dao.ViedoDao;

import java.util.List;

public class ViedoImpl extends BaseDao implements ViedoDao {
    //视频添加
    @Override
    public int addViedo(Viedo viedo) {
        String sql = "INSERT INTO `viedo`(`vname`,`vtype`,`vdesc`,`pic`,`upLoader`,`path`)VALUES (?,?,?,?,?,?)";
        return update(sql, viedo.getVname(), viedo.getVtype(), viedo.getVdesc(), viedo.getPic(), viedo.getUpLoader(), viedo.getPath());
    }

    //视频删除
    @Override
    public int deleteViedoid(int id) {
        String sql = "DELETE FROM `viedo`WHERE `id`=?";
        return update(sql, id);
    }

    //视频修改
    @Override
    public int updataViedo(Viedo viedo) {
        String sql = "UPDATE viedo SET `vname`=?,`vtype`=?,`vdesc`=?,`pic`=?,`number`=?,`idDown`=?,`upLoader`=?,`checkid`=?,`uploaddata`=?,`checkdata`=?,`favoriteNum`=?,`likeNum`=? ,`path`=?  WHERE `id`=? ";
        return update(sql, viedo.getVname(), viedo.getVtype(), viedo.getVdesc(), viedo.getPic(), viedo.getNumber(), viedo.getIdDown(), viedo.getUpLoader(), viedo.getCheckid(), viedo.getUploaddata(), viedo.getCheckdata(), viedo.getFavoriteNum(), viedo.getLikeNum(), viedo.getPath(),viedo.getId());
    }
//查出所属类型的视频
    @Override
    public List<Viedo> queryViedoTYPE(String vtype) {
        String sql = null;
        if (vtype == null) {
            sql = "SELECT `id`,`vname`,`vtype`,`vdesc`,`pic`,`number`,`idDown`,`upLoader`,`checkid`,`uploaddata`,`checkdata`,`favoriteNum`,`likeNum`,`path` FROM `viedo`";

        } else {

            sql = "SELECT `id`,`vname`,`vtype`,`vdesc`,`pic`,`number`,`idDown`,`upLoader`,`checkid`,`uploaddata`,`checkdata`,`favoriteNum`,`likeNum`,`path` FROM `viedo` WHERE  vtype =  ?";
        }


        return queryForList(Viedo.class, sql, vtype);

    }

    //根据名字查询
    @Override
    public List<Viedo> queryViedovname(String vname) {
        String sql = null;
        if (vname == null) {
            sql = "SELECT `id`,`vname`,`vtype`,`vdesc`,`pic`,`number`,`idDown`,`upLoader`,`checkid`,`uploaddata`,`checkdata`,`favoriteNum`,`likeNum`,`path` FROM `viedo`";

        } else {
            vname = "%" + vname + "%";
            sql = "SELECT `id`,`vname`,`vtype`,`vdesc`,`pic`,`number`,`idDown`,`upLoader`,`checkid`,`uploaddata`,`checkdata`,`favoriteNum`,`likeNum`,`path` FROM `viedo` WHERE  vname LIKE  ?";
        }


        return queryForList(Viedo.class, sql, vname);

    }

    //查询全部视频
    @Override
    public List<Viedo> queryViedoAll() {
        String sql = "SELECT `id`,`vname`,`vtype`,`vdesc`,`pic`,`number`,`idDown`,`upLoader`,`checkid`,`uploaddata`,`checkdata`,`favoriteNum`,`likeNum` FROM `viedo`";
        return queryForList(Viedo.class, sql);
    }

    //查询总页数
    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(1) from `viedo`";
        //调用查询单个sql语句，查询数量
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue(); //intValue方法把Number类型转成Integer类型
    }

    //查询某页的数据
    @Override
    public List<Viedo> queryForPageItems(String name, int begin, int pageSize) {
        String sql = null;
        if (name == null) {
            sql = "SELECT `id`,`vname`,`vtype`,`vdesc`,`pic`,`number`,`idDown`,`upLoader`,`checkid`,`uploaddata`,`checkdata`,`favoriteNum`,`likeNum`,`path` FROM `viedo` LIMIT ?,?";
            return queryForList(Viedo.class, sql, begin, pageSize);
        } else {
            name = "%" + name + "%";
            sql = "SELECT `id`,`vname`,`vtype`,`vdesc`,`pic`,`number`,`idDown`,`upLoader`,`checkid`,`uploaddata`,`checkdata`,`favoriteNum`,`likeNum`,`path` FROM `viedo` where vname like ? LIMIT ?,?";
            return queryForList(Viedo.class, sql, name, begin, pageSize);
        }

    }

//类型视频查询
    @Override
    public List<Viedo> queryForPageType(String vtype, int begin, int pageSize) {
        String sql = null;
        if (vtype == null) {
            sql = "SELECT `id`,`vname`,`vtype`,`vdesc`,`pic`,`number`,`idDown`,`upLoader`,`checkid`,`uploaddata`,`checkdata`,`favoriteNum`,`likeNum`,`path` FROM `viedo` LIMIT ?,?";
            return queryForList(Viedo.class, sql, begin, pageSize);
        } else {
            sql = "SELECT `id`,`vname`,`vtype`,`vdesc`,`pic`,`number`,`idDown`,`upLoader`,`checkid`,`uploaddata`,`checkdata`,`favoriteNum`,`likeNum`,`path` FROM `viedo` where vtype =?";
            return queryForList(Viedo.class, sql, vtype, begin, pageSize);
        }
    }

    //按播放量查询页面总数
    @Override
    public Integer queryForPageTotalCountByPrice(int min, int max) {
        String sql = "select count(*) from viedo where number between ? and ?";
        Number count = (Number) queryForSingleValue(sql, min, max);
        return count.intValue();
    }

    /**
     * 根据视频id查找 显示在single.jsp的窗格上
     *
     * @param id
     * @return
     */
    @Override
    public Viedo findVideoById(Integer id) {
        String sql = "select * from viedo where id =?";
        return queryForOne(Viedo.class, sql, id);
    }

    /**
     * 保存video的方法
     *
     * @param viedo
     */
    @Override
    public int frontSave(Viedo viedo) {

     String sql = "insert into `viedo` (vname, vtype, vdesc, pic, number, idDown, upLoader, checkid, uploaddata, checkdata, favoriteNum,  likeNum ,path) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    return update(sql,viedo.getVname(),viedo.getVtype(),viedo.getVdesc(),viedo.getPic(),viedo.getNumber(),viedo.getIdDown(),viedo.getUpLoader(),viedo.getCheckid(),viedo.getUploaddata(),viedo.getCheckdata(),viedo.getFavoriteNum(),viedo.getLikeNum(),viedo.getPath());
    }

    /**
     * 根据用户id查询收藏的视频
     *
     * @param userId
     * @return
     */
    @Override
    public List<Viedo> findFavoriteVideoByUsersId(Integer userId) {
        String sql="select * from viedo where id in (select viedoId from history where userId = ?";
        return queryForList(Viedo.class,sql,userId);
    }

    /**
     * 根据用户id查询历史浏览的视频
     *
     * @param userId
     * @return
     */
    @Override
    public List<Viedo> findHistoryVideoByUsersId(Integer userId) {
        String sql= "select * from video where id in (select videoId from history where userId =?)";
        return queryForList(Viedo.class,sql,userId);
    }

    /**
     * 根据用户id查询点赞视频
     *
     * @param userId
     * @return
     */
    @Override
    public List<Viedo> findLikeVideoByUserId(Integer userId) {
        String sql="select * from video where id in (select videoId from likeList where userId=?)";
        return queryForList(Viedo.class,sql,userId);
    }
    // 按价格查询页面项目
//    @Override
//    public List<Viedo> queryForPageItemsByPrice(int begin, int pageSize, int min, int max) {
//        String sql = "select `id`,`name`,`producer`,`price`,`sales`,`stock`,`img_path` " +
//                "from t_goods where price between ? and ? order by price limit ?,?";
//        return queryForList(Viedo.class, sql, min, max, begin, pageSize);
//    }


}
